package javaWeb;

import Bean.*;
import Service.CartService;
import Service.FurnService;
import Service.Impl.CartServiceImpl;
import Service.Impl.FurnServiceImpl;
import Service.Impl.OrderServiceImpl;
import Service.OrderService;
import Utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class OrderServlet extends BasicServlet{
    private FurnService furnService = new FurnServiceImpl();
    private CartService cartService = new CartServiceImpl();
    private OrderService orderService = new OrderServiceImpl();

    protected void endCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        //还要判断是否是空订单
        String sql = "select id, FurnName, Merchant, Price, Volume, inventory, img_path imgPath from Furn where id = ?";
        List<Cart.CartGood> cartGoods = cartService.getAllCartFurn(user.getId());
        if (cartGoods == null || cartGoods.size() == 0){
            resp.sendRedirect("/Web/javaWeb/CustomerFurnPage?action=FurnPage");
            return;
        }

        // 先添加一个订单
        Order order = new Order();
        order.setOrderNumber(String.valueOf(Tool.getOrderNumber()));
        order.setUserId(user.getId());
        order.setSumPrice(furnService.calculatePrice(user.getId()));
        orderService.insertOrder(order);
        // 插入家居用一个service完成
        //先在这里写

        StringBuffer statue =  new StringBuffer();

        for (Cart.CartGood good :cartGoods) {
            Furn furn = furnService.getFurn(sql, good.FurnId);
            furn.setVolume(furn.getVolume()+good.amount);
            if (furn.getInventory()-good.amount < 0){
                furn.setInventory(0);
                statue.append(furn.getFurnName()+"缺货中,等待补货在发货" +" ");
            }else {
                furn.setInventory(furn.getInventory()-good.amount);
            }
            furnService.updateFurn(furn.getId(),furn);
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderNumber(order.getOrderNumber());
            orderItem.setAmount(good.getAmount());
            orderItem.setFurnName(furn.getFurnName());
            orderItem.setPrice(furn.getPrice());
            BigDecimal sum = new BigDecimal(good.amount);
            sum = sum.multiply(furn.getPrice());
            orderItem.setSumPrice(sum);
            orderService.insertOrderItem(orderItem);
        }
        String word = new String(statue);
        orderService.updateStatue(word, user.getId(),order.getOrderNumber(),order.getDatetime());
        req.setAttribute("OrderNumber",order.getOrderNumber());
       req.getRequestDispatcher("/javaWeb/CartServlet?action=clearOrder").forward(req,resp);
    }



    protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        List<Order> orders = orderService.getALLOrder(user.getId());
        session.setAttribute("Orders",orders);
        resp.sendRedirect("/Web/Order/order.jsp");
    }

    protected void showOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        String orderNumber = req.getParameter("orderNumber");
        List<OrderItem> orderALL = orderService.getOrderALL(orderNumber);
        session.setAttribute("OrderItem",orderALL);
        resp.sendRedirect("/Web/Order/order_detail.jsp");
    }

}
